import java.util.List;

public class DaoCanvas extends JavaClass{
	
	private String className;
	
	private StringBuilder content = new StringBuilder();

	private StringBuilder sb = new StringBuilder();
	
	public DaoCanvas(BaseData baseData,String packageName,List<String> imports) {
		super();
		this.packageName = packageName;
		this.className = baseData.getClassName();
		this.imports = imports;
		initHead();
		initFunction();
	}
	
	private void initHead() {
		content.append("package ").append(packageName).append(";\n\n");
		
		for (String imp : imports) {
			content.append("import ").append(imp).append(";\n");
		}
		
		content.append("\npublic interface ").append(className).append("Dao");
		content.append(" {\n").append("\n}");
	}
	
	private void initFunction() {
		add();
		delete();
		update();
		find();
		content.insert(getEndInput(), sb);
	}
	
	private void add() {
		sb.append("\n\tpublic void add(").append(className).append(" ").append(JavaTool.toLowerCaseFirstOne(className)).append(");\n");
	}
	
	private void delete() {
		sb.append("\n\tpublic int delete(String id);\n");
	}
	
	private void update() {
		sb.append("\n\tpublic int update(").append(className).append(" ").append(JavaTool.toLowerCaseFirstOne(className)).append(");\n");
	}
	
	private void find() {
		sb.append("\n\tpublic ").append(className).append(" findById(String id);\n");
		sb.append("\n\tpublic List<").append(className).append("> findByData(").append(className).append(" ").append(JavaTool.toLowerCaseFirstOne(className)).append(");\n");
	}
	
	public void readContent() {
		System.out.println(content);
	}
	
	private int getEndInput() {
		return content.lastIndexOf("}")-1;
	}

	public StringBuilder getContent() {
		return content;
	}

}
